<?php
namespace  v1;

class User extends \ResourceClass{
    public $isActive = true;
    public $remark = '微信授权获取用户信息';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['openid'])) {
            return 'require openid ';
        }
        try {
            // 查询数据库，有信息就返回
            $user_class = new \Orm\User\UserClass();
            $user_class->loadByOpenId($req['openid']);
            $result['id'] = $user_class['id'];
            $result['openid'] = $user_class['openid'];
            $result['nickname'] = $user_class['nickname'];
            $result['headimgurl'] = $user_class['headimgurl'];
            $result['sex'] = $user_class['sex'];
            $result['city'] = $user_class['city'];
            $result['province'] = $user_class['province'];
            $result['country'] = $user_class['country'];

        }catch (\Exception $e) {
            $result = array();
        }
    }

    public function post(array $req, array $post, &$result)
    {
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['openid'])) {
            return 'openid不能为空';
        }
        if (!isset($data['nickname'])) {
            return '用户名不能为空';
        }
        if (!isset($data['headimgurl'])) {
            return '头像不能为空';
        }
        $transaction = new \Base\BaseOrmModel();
        $transaction->beginTransaction();
        try {
            $user_class = new \Orm\User\UserClass();

            $user_class['openid'] = $data['openid'];
            $user_class['nickname'] = $data['nickname'];
            $user_class['headimgurl'] = $data['headimgurl'];
            $user_class['sex'] = $data['sex'];
            $user_class['city'] = $data['city'];
            $user_class['province'] = $data['province'];
            $user_class['country'] = $data['country'];
            $user_class->insert();

            $user_account = new \Orm\UserAccount\UserAccountClass();
            $user_account['uid'] = $user_class['id'];
            $user_account['nickname'] =  $data['nickname'];
            $user_account->insert();
            $result = array(
                'id' => $user_class['id'],
                'openid' => $data['openid'],
                'nickname' => $data['nickname'],
                'headimgurl' => $data['headimgurl'],
            );
            $transaction->commit();
        }catch (\Exception $e ) {
            $transaction->rollBack();
            return $e->getMessage();
        }
        return '';
    }

    public function put(array $req, array $post, &$result)
    {
        if (isset($req['id']) == false || intval($req['id']) == 0) {
            return '用户id 不能为空';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);

        try {
            $user_class = new \Orm\User\UserClass();
            $user_class->loadById($req['id']);

            if (isset($data['openid'])) {
                $user_class['openid'] = $data['openid'];
            }
            if (isset($data['nickname'])) {
                $user_class['nickname'] = $data['nickname'];
            }
            if (isset($data['headimgurl'])) {
                $user_class['headimgurl'] = $data['headimgurl'];
            }
            $user_class->update();
            $result = array(
                'id' => $user_class['id'],
                'openid' => $user_class['openid'],
                'nickname' => $user_class['nickname'],
                'headimgurl' => $user_class['headimgurl'],
            );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }
}